package service4admin

import (
	"Gin-Gorm-boke/boke_common/models"
	"Gin-Gorm-boke/boke_common/response"
	"github.com/gin-gonic/gin"
	"strconv"
)

func SaveLink(c *gin.Context) {
	var link models.Link
	_ = c.ShouldBindJSON(&link)
	models.Db.Create(&link)
	response.ResponseOK(c, nil)
	return

}

func UpdateLink(c *gin.Context) {
	var link models.Link
	_ = c.ShouldBindJSON(&link)
	models.Db.Model(new(models.Link)).Where("id=?", link.Id).Updates(&link)
	response.ResponseOK(c, nil)
	return

}

func LinkPageList(c *gin.Context) {
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	name := c.Query("name")
	status := c.Query("status")
	db := models.Db.Model(new(models.LinkVo))
	if name != "" {
		db.Where("name like ?", "%"+name+"%")
	}
	if status != "" {
		db.Where("status = ?", status)
	}
	var total int64
	db.Count(&total)
	var linkList []models.LinkVo
	db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&linkList)
	response.ResponseOK(c, gin.H{
		"total": total,
		"rows":  linkList,
	})
	return

}

func GetLinkById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	var link models.LinkVo
	models.Db.First(&link, id)
	response.ResponseOK(c, link)
	return

}

func DelLinkById(c *gin.Context) {
	id, _ := strconv.Atoi(c.Param("id"))
	models.Db.Model(new(models.Link)).Where("id=?", id).Update("del_flag", "1")
	models.Db.Delete(new(models.Link), id)
	response.ResponseOK(c, nil)
	return

}
